import api from '../../api/index';
import { sdk } from '../../config/sdk';
Page({
  data: {
    loading: true
  },
  onLoad: function (options) {
    this.getBanner();
    this.getMenu();
    this.getNotice();
  },

  // 获取轮播图
  async getBanner() {
    const { code, message, data } = await sdk.request({ url: api.getBanner, method: 'GET' });
    if (code != 1000) return this.$error(message);
    this.setData({
      banner: data
    });
  },

  // 获取菜单
  async getMenu() {
    const { code, message, data } = await sdk.request({ url: api.getMenu, method: 'GET' });
    if (code != 1000) return this.$error(message);
    this.setData({
      menu: data,
      loading: false
    });
  },

  // 获取公告
  async getNotice() {
    const { code, message, data } = await sdk.request({ url: api.getNotice, method: 'GET' });
    if (code != 1000) return this.$error(message);
    this.setData({
      notice: data
    });
  },

  // 点击通知
  Notice(e) {
    this.$showDialog({
      title: '公 告',
      content: this.data.notice[e.detail.current].label,
      confirmText: false
    });
  },

  // 点击菜单
  async tapTo(e) {
    const { type, path, appId, login, stuauth, vip } = e.currentTarget.dataset.item;
    // 登录
    const Login = this.checkLogin(login);
    if (!Login) {
      return this.$showDialog({
        title: '系统提示',
        content: '您还未登录 请先登录',
        confirmText: '立即登录',
        success: res => {
          if (res.confirm) {
            this._toPath('pages/login/index');
          }
        }
      })
    }
    //获取用户信息
    const { code, message, data } = await sdk.request({ url: api.getUserInfo, method: 'GET' });
    if (code != 1000) return this.$error(message);
    wx.setStorageSync('remind', data.remind);
    // 学生认证
    const Auth = await this.checkStuAuth(stuauth, data);
    if (!Auth) {
      return this.$showDialog({
        title: '系统提示',
        content: '未绑定学号 暂无法使用该服务',
        confirmText: '立即认证',
        success: res => {
          if (res.confirm) {
            this._toPath('pages/auth/index');
          }
        }
      })
    }

    // 权限认证
    const Vip = await this.checkVip(vip, data);
    if (!Vip) {
      return this.$showDialog({
        title: '系统提示',
        content: '仅对会员用户开放，请前往会员中心了解',
        confirmText: '免费开通',
        success: res => {
          if (res.confirm) {
            this._toPath('pages/vip/index');
          }
        }
      })
    }

    // 执行跳转
    if (Login && Auth && Vip) {
      if (type == 'nav') return this._toPath(path);
      if (type == 'mp') return this._toPath(path, appId, 'ToMiniProgram');
    }
  },

  // 登录检测
  checkLogin(login) {
    if (login) {
      const userlogin = wx.getStorageSync('login');
      if (login == userlogin) {
        return true;
      }
      return false;
    } else {
      return true;
    }
  },

  // 学生认证检测
  async checkStuAuth(stuauth, data) {
    if (stuauth) {
      if (data.username && data.password) {
        return true;
      }
      return false;
    } else {
      return true;
    }
  },

  // 权限检测
  async checkVip(vip, data) {
    if (vip) {
      if (data.expire) {
        const nowTime = new Date().getTime();
        const vipExpire = Date.parse(data.expire);
        if (vipExpire - nowTime > 0) {
          return true;
        }
      }
      return false;
    } else {
      return true;
    }
  }
})